package com.darkplanet.management.controller;

import com.darkplanet.management.domain.Employee;
import com.darkplanet.management.domain.Project;
import com.darkplanet.management.domain.SecondMenu;
import com.darkplanet.management.service.EmployeeMenuImp;
import com.darkplanet.management.service.EmployeeServiceImp;
import com.darkplanet.management.service.ProjectService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

@WebServlet(urlPatterns = "/LoginServlet")
public class LoginServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request,response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setCharacterEncoding("utf-8");
        String username = request.getParameter("username");
        String pwd = request.getParameter("pwd");
        EmployeeServiceImp employeeService = new EmployeeServiceImp();
        Employee employee = employeeService.Login(username, pwd);
        if(employee!=null){
            HttpSession session = request.getSession();
            session.setAttribute("employee",employee);
            EmployeeMenuImp employeeMenu = new EmployeeMenuImp();
            List<List> menus = employeeMenu.getMenus(employee.getEmployee_id());
            List<SecondMenu> menuList = menus.get((1));
            session.setAttribute("Menus",menus);
            for (SecondMenu secondMenu : menuList) {
                int menu_id = secondMenu.getS_menu_id();
                if(menu_id==13){
                    List<Employee> employees = employeeService.getEmployees();
                    request.getSession().setAttribute("userlist",employees);
                }
                if(menu_id==1){
                    ProjectService projectService = new ProjectService();
                    List<Project> allProject = projectService.getAllProject();
                    session.setAttribute("allProject",allProject);
                }
                }
            }
        else {
           response.getWriter().print("false");
    }
    }
}
